import { login, logout, getInfo, getSystemMenu } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    nickname: '',
    username:'',
    head_img: '',
    roles: [],
    menus: {}
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NICKNAME: (state, nickname) => {
    state.nickname = nickname
  },

  SET_USERNAME: (state, username) => {
    state.username = username
  },

  SET_AVATAR: (state, avatar) => {
    state.head_img = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_MENU: (state, menus) => {
    state.menus = menus
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ 	
        username: username.trim(),
        password: password 
      }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(res => {
        const { data } = res
        if (!data) {
          reject('Verification failed, please Login again.')
        }

        console.log('获取用户信息===============》',data);
		
        let roles = data.roles

        roles = ['admin']
        let head_img = data.head_img || 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'

        if (!roles || roles.length <= 0) {
          reject('getInfo: roles must be a non-null array!')
        }

        commit('SET_ROLES', roles)
        commit('SET_USERNAME', data.username)
        commit('SET_NICKNAME', data.nickname)
        commit('SET_AVATAR', head_img)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取路由
  getRouter({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      getSystemMenu().then(response => {
        const menus = response.data
        console.log('路由数据==》', response.data)
        // 如果需要404 页面，请在此处添加
        menus.push({
          path: '/404',
          component: '404',
          hidden: true
        }, {
          path: '*',
          redirect: '/404',
          hidden: true
        })
        commit('SET_MENU', menus) // 把name 保存到vuex中
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // user logout
  logout({ commit, state }) {
    removeToken() // must remove  token  first
    resetRouter()
    return
    // return new Promise((resolve, reject) => {
    //   logout(state.token).then(() => {
    //     console.log(8888)
    //     removeToken() // must remove  token  first
    //     resetRouter()
    //     commit('RESET_STATE')
    //     resolve()
    //   }).catch(error => {
    //     reject(error)
    //   })
    // })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

